package com.catering.service.serviceImpl;

import com.catering.domain.DishOrder;
import com.catering.domain.OrderList;
import com.catering.domain.Orders;
import com.catering.mapper.OrderListMapper;
import com.catering.mapper.OrderMapper;
import com.catering.project.domain.OrderData;
import com.catering.service.OrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

@Service
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListMapper orderListMapper;

    @Autowired
    private OrderMapper orderMapper;


    //获取所有今日订单的营业额和订单数量
    @Override
    public Map<String, Object> getAllOrderList() {
        Map<String,Object> map = new HashMap<>();
        double totalPrice = 0;
        int num = 0;
        Long  time = System.currentTimeMillis();  //当前时间的时间戳
        long zero = time/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        System.out.println(new Timestamp(zero));//今天零点零分零秒
        System.out.println(zero/1000);
        Date yesterday = new  Timestamp(zero);

        List<OrderList>  orderLists = orderListMapper.getAllOrderList();
        List<OrderList>  list = new ArrayList<>();
        for (OrderList l : orderLists) {
            boolean isInRange = l.getCreatetime().after(yesterday);
            if (isInRange){
                list.add(l);
            }
        }
        for (OrderList l : list){
            totalPrice+=l.getTotalprice();
            num++;
        }
        map.put("totalPrice",totalPrice);
        map.put("num",num);
        return map;
    }

    //所有订单详细信息
    @Override
    public Map<String, Object> getDetailsOrder() {
        int total = 0;
        Map<String, Object> map = new HashMap<>();
        List<Orders> list = orderListMapper.getDetailsOrder();
        for (Orders o : list) {
            total++;
        }
        map.put("list",list);
        map.put("total",total);
        return map;
    }



    //今日订单详细信息
    @Override
    public List<Orders> getTodayDetailsOrder() {

        Long  time = System.currentTimeMillis();  //当前时间的时间戳
        long zero = time/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        System.out.println(new Timestamp(zero));//今天零点零分零秒
        System.out.println(zero/1000);
        Date yesterday = new  Timestamp(zero);
        List<Orders>  orderLists = orderListMapper.getDetailsOrder();
        List<Orders>  list = new ArrayList<>();
        for (Orders l : orderLists) {
            System.out.println(l);
            boolean isInRange = l.getCreatetime().after(yesterday);
            if (isInRange){
                list.add(l);
            }
        }

        return list;
    }

    //订单中所下单菜品信息
    @Override
    public List<DishOrder> getDishOrder() {
        return orderListMapper.getDishOrder();
    }

    @Override
    //获取订单中所下单的菜品
    public List<DishOrder> getDishOrderById(Integer id){
        return orderListMapper.getDishOrderById(id);
    }

    //获取最近的一单订单信息
    @Override
    public Orders getNewOrders(){
        List<Orders> list = getTodayDetailsOrder();
        if (list==null){
            return null;
        }
        return list.get(list.size() - 1);
    }

    //模糊搜索所有订单符合条件的订单
    @Override
    public List<Orders> getOrderLike(String message){
        return orderListMapper.getOrderLike(message);
    }



}
